package com.ywyself.service;

import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.ywyself.dao.DiaryDao;
import com.ywyself.ext.kit.YsKit;
import com.ywyself.ext.utils.YsStringUtils;
import com.ywyself.model.Diary;

/**
 *
 * @author YuWenYue
 * @date 2017年11月6日 下午8:02:03
 */
public class DiaryService {
    private static final DiaryDao dao = new DiaryDao();

    public static final DiaryService EXC = new DiaryService();

    /**
     * 增加
     * 
     * @param diary
     * @return
     */
    public YsKit save(String content, String author, String remark) {
        if (YsStringUtils.isBlank(content)) {
            return YsKit.fail().code("300").msg("内容不能为空");
        }
        Diary diary = new Diary();
        diary.setContent(content);
        diary.setAuthor(author);
        diary.setRemark(remark);
        return dao.save(diary);
    }

    /**
     * 删除
     * 
     * @param id
     * @return
     */
    public boolean delete(long id) {
        return dao.delete(id);
    }

    /**
     * 更新
     * 
     * @param id
     * @param content
     * @param author
     * @param remark
     * @return
     */
    public YsKit update(long id, String content, String author, String remark) {
        if (YsStringUtils.isBlank(content)) {
            return YsKit.fail().code("300").msg("内容不能为空");
        }
        return dao.update(id, content, author, remark);
    }

    /**
     * 基于id查询数据
     * 
     * @param id
     * @return 一个对象，不会为null
     */
    public Diary get(long id) {
        Diary diary = dao.get(id);
        if (diary == null) {
            return new Diary();
        } else {
            return diary;
        }
    }

    /**
     * 查询
     * 
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public Page<Record> page(int pageNumber, int pageSize) {
        return dao.page(pageNumber, pageSize);
    }

}
